package com.seari.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class LoadPropertiesUtils {

	private static final String PROPERTIES_NAME = "config.cfg";
	public static Properties properties;

	public static synchronized Properties loadInfos() throws IOException {
		FileInputStream in = null;
		try {
			if(properties != null)
			{
				return properties;
			}else {
				properties = new Properties();
				in = new FileInputStream(PROPERTIES_NAME);
				properties.load(in);
				return properties;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
		}
		return null;
	}
	
	
	public static String[] getMidWares() {
		Properties propertieInfo = null;
		try {
			//从配置文件取监控中间件名称
			propertieInfo = loadInfos(); 
			String middlewares = propertieInfo.getProperty("middleware");
			if(middlewares!= null && !middlewares.equals("")) {
				String[] split = middlewares.split(",");
				return split;
			}
		} catch (IOException e) {
		}
		return null;
	}
	
	public static Map<String,String> getItemDescription(String key){
		Map<String,String>  res = new HashMap<String,String>();
		try {
			Properties loadInfos = loadInfos();
			String strValue = loadInfos.getProperty(key);
			if(strValue==null || strValue.equals("")) {
				return null;
			}
			String[] split = strValue.split(",");
			res.put("item", split[0]);
			res.put("scriptPath", split[1]);
			return res;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void main(String[] args) throws IOException {
		Properties loadInfos = loadInfos();
		Enumeration<?> propertyNames = loadInfos.propertyNames();
		while(propertyNames.hasMoreElements()) {
			String strKey = (String) propertyNames.nextElement();
			 String strValue = loadInfos.getProperty(strKey);
			 System.out.println(strKey + "=" + strValue);
		}
		
		Set<Object> keySet = loadInfos.keySet();
		Iterator<Object> iterator = keySet.iterator();
		while(iterator.hasNext()) {
			Object next = iterator.next();
			System.out.println(next);
		}
		System.out.println("------------------");
		
		Map<String, String> itemDescription = getItemDescription("stringKe");
		System.out.println(itemDescription);
	}

}
